/*
 * CStack.cpp
 *
 *  Created on: Sep 23, 2014
 *      Author: hung
 */

#include "CStack.h"
#include <iostream>
using namespace std;

CStack::CStack() {
	pTop = NULL;
	size = 0;
}

bool CStack::push(char data) {
	Node* pNew = new Node();
	pNew->next = NULL;
	pNew->data = data;
	if (pTop == NULL) {
		pTop = pNew;
		size++;
		return true;
	} else {
		pNew->next = pTop;
		pTop = pNew;
		size++;
		return true;
	}
}

bool CStack::pop(char& data) {
	if (pTop == NULL)
		return false;
	Node* pDel = pTop;
	pTop = pTop->next;
	data = pDel->data;
	delete pDel;
	size--;
	return true;
}

char CStack::top() {
	if (pTop == NULL)
		return ' ';
	else
		return pTop->data;
}

void CStack::view() {
	if (pTop == NULL)
		cout << "Stack EmPT" << endl;
	else {
		Node* pt = pTop;
		while (pt != NULL) {
			if (pt == pTop) {
				cout << "(top)" << pt->data << " ";
			} else {
				if (pt->next == NULL)
					cout << pt->data;
				else
					cout << pt->data << " ";
			}

			pt = pt->next;
		}
	}

	cout << endl;
}

CStack::~CStack() {
	// TODO Auto-generated destructor stub
}

